// pages/chat/chat.js
const app = getApp();
var funcs = require('../../static/js/funcs.js');
Page({

  /**A
   * 页面的初始数据
   */
  data: {
    tishi: "请选择群组和日期",
    select_date: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    let openid = app.globalData.openid
    funcs.getGroupAdmin(openid).then(res=>{
      if(res.data.succeed){
        let grouplist = res.data.data.groups
        console.log(grouplist)
        that.setData({
          grouplist: grouplist
        })
        if(grouplist.length>0){
          that.setData({
            selectedgroup: grouplist[0].groupId,
            select_date:grouplist[0].status
          })
          this.fresh()
        }
      }
    })
  },
  // 改变日期
  select_date_func: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    this.setData({
      select_date:e.detail.value
    })
    if(this.data.selectedgroup!=null&&this.data.select_date!=null){
      this.fresh()
    }
  },
  select_group_button(e){
    let groupid = e.currentTarget.dataset.groupid;
    this.setData({
      selectedOpenDate:e.currentTarget.dataset.displaystatus
    })
    console.log("选中的组群为：" + groupid)
    let that = this
    this.setData({
      selectedgroup: groupid
    })
    if(this.data.selectedgroup!=null&&this.data.select_date!=null){
      this.fresh()
    }else if(this.data.selectedgroup!=null&&this.data.select_date==null){
      let openid = app.globalData.openid
      funcs.getAssGroup(openid,this.data.selectedgroup,"").then(res=>{
        if(res.data.succeed){
          if(res.data.data.code==true){
            that.setData({
              openFlag:true,
              openedDate: this.data.select_date
            })
          }else{
            let date = res.data.data.date;
            that.setData({
              openFlag:false,
              openedDate: date
            })
          }
        }else{
          that.setData({
            openFlag:false,
            openedDate:null
          })
        }
      })
    }
  },

  fresh(){
    let that = this
    console.log("获取画像详情请求")
    let openid = app.globalData.openid
    wx.showLoading({
      title: '加载中',
    })
    funcs.getAssGroup(openid,this.data.selectedgroup,this.data.select_date).then(res=>{
      if(res.data.succeed){
        if(res.data.data.code==true){
          that.setData({
            openFlag:true,
            openedDate: this.data.select_date
          })
        }else{
          let date = res.data.data.date;
          that.setData({
            openFlag:false,
            openedDate: date
          })
        }
      }else{
        that.setData({
          openFlag:false,
          openedDate:null
        })
      }
      funcs.getMemberAdmin(openid,this.data.selectedgroup,this.data.select_date).then(res=>{
        if(res.data.succeed){
          let members = res.data.data.members
          
          console.log(members)
          that.setData({
            members:members,
          })
        }else{
          console.log("拉去数据失败")
          // that.setData({
          //   tishi:"不在本次参评范围内"
          // })
        }
        wx.hideLoading() 
      })
    })
  },

  openAssess(){
    let openid = app.globalData.openid
    let that = this
    if(this.data.selectedgroup!=null&&this.data.select_date!=null){
      funcs.openAssGroup(openid,this.data.selectedgroup,this.data.select_date).then(res=>{
        if(res.data.succeed){
          wx.showToast({
            title: '开启评测成功',
            icon: "success",
            duration: 2000
          })
          that.fresh()
        }else{
          wx.showToast({
            title: '开启评测失败',
            icon: "error",
            duration: 2000
          })
        }
      })
    }
  },
  closeAssess(){
    let openid = app.globalData.openid
    let that = this
    if(this.data.selectedgroup!=null&&this.data.select_date!=null){
      funcs.closeAssGroup(openid,this.data.selectedgroup,this.data.select_date).then(res=>{
        if(res.data.succeed){
          wx.showToast({
            title: '关闭评测成功',
            icon: "success",
            duration: 2000
          })
          that.fresh()
        }else{
          wx.showToast({
            title: '关闭评测失败',
            icon: "error",
            duration: 2000
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getDeail(e){
    let data = e.currentTarget.dataset;
    // console.log(dataset)
    // let groupid = e.currentTarget.dataset.dataset;
    console.log(data)
    var jsonString = JSON.stringify(data);
    //跳转页面并传递json字符串
    wx.navigateTo({
      url: '/pages/assdisplay/assdisplay?params=' + encodeURIComponent(jsonString)
    })
  },

  extractReport(){
    let openid = app.globalData.openid
    funcs.extractReport(openid,this.data.selectedgroup,this.data.select_date)
  },

  getdaiban(e){
    let data = e.currentTarget.dataset;
    // console.log(dataset)
    // let groupid = e.currentTarget.dataset.dataset;
    console.log(data)
    var jsonString = JSON.stringify(data);
    wx.navigateTo({
      url: '/pages/daiban/daiban?params=' + encodeURIComponent(jsonString)
    })
  },
  cuiban(){
    this.errorNotice()
  },
  errorNotice: function (res) {
    wx.showToast({
      title: "因数据安全考虑\r\n目前暂不开放该功能",
      icon: 'none',
      duration: 2000
    })
  },
})